package isnork.g2;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import isnork.g2.utility.Utility;
import isnork.sim.BoardPanel;
import isnork.sim.GameObject;
import isnork.sim.Observation;
import isnork.sim.SeaLifePrototype;
import isnork.sim.GameObject.Direction;

public class Route implements Strategy {
	Double expHappDensity;
	double x, y;
	Point2D p;
	int tick = 0;
	int finalTick = ExplorerConfig.FinalTick;
	double d;
	double r;
	double id;
	double n;
	double offset;
	boolean done = false;
	int i = 1;
	int lastTime;
	int timeNeed;
	private double utility;
	double PI=Math.PI;

	public Route(G2Explorer diver) {
		d = diver.d;
		r = diver.r;
		n = diver.n;
		id = Math.abs(diver.getId());
		double degree;
		double r2 = d-r+1;
		double r3 = d-r*3+1;
		
		if(d>50){
			
		}
		int c = (int) ((d-r)/r/2);
		
		for(int i=0;i<=c;i++){
			r2 =(d - r - 2*i*r - 2*r*((int)(id)%(c+1)));
			if(r2<=0){
				r2+=d-r;
			}

			for(degree=id*2*PI/n;degree<2*PI+id*2*PI/n;degree=degree+PI/10){
				if(Math.abs(Math.sin(degree))>Math.abs(Math.cos(degree)))
					diver.toView.add(new Point2D.Double((r2)*Math.signum(Math.sin(degree)),(r2)*Math.cos(degree)));
				else
					diver.toView.add(new Point2D.Double((r2)*Math.sin(degree),(r2)*Math.signum(Math.cos(degree))));
			}			
		}


		
		diver.first = diver.toView.remove();
	}

	@Override
	public Map<Direction, Double> process(G2Explorer diver) {
		
		utility = diver.expHapp*(diver.safeDegree-0.5)/480
				* ExplorerConfig.ROUTE;

		Map<Direction, Double> utility_accumulatorExp = new HashMap<Direction, Double>();
		if (done){
			//utility_accumulatorExp.put(Direction.STAYPUT, utility);
			//return utility_accumulatorExp;
			return null;
		
		}
		p = diver.first;
			
		Direction dir = Utility.RelativeDirection(diver.location, p);
		
		for(Direction d : Direction.values())
		{  
			if(d==Direction.STAYPUT){
				
			}
			else if (Math.abs(d.getDegrees()-diver.lastDirection.getDegrees())<180 && d == dir)
			{
				utility_accumulatorExp.put(d, utility);
			}
			else if (Math.abs(d.getDegrees()-diver.lastDirection.getDegrees())<180 && Math.abs(d.getDegrees()-dir.getDegrees())<=45)
			{
				utility_accumulatorExp.put(d, utility*ExplorerConfig.secondChoice);
			}
			else if (Math.abs(d.getDegrees()-diver.lastDirection.getDegrees())<180 && Math.abs(d.getDegrees()-dir.getDegrees())<=90)
			{
				utility_accumulatorExp.put(d, utility*ExplorerConfig.thirdChoice);
			}
		}
		if (p.distance(diver.location) < r/2 || (diver.lastLocation.distance(diver.location)<5 && diver.current_round%20 >= 15)) {
			if (diver.toView.isEmpty()) {
				done = true;
			} else{				
				diver.first = diver.toView.remove();
			}
				
		}
		return utility_accumulatorExp;

	}
}